﻿using _698._45Tool.Utils;
using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Jn698._45GWTool.Properties;
using Jn698._45GWTool.Views;
using nblTool.utils;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using TouchSocket.Sockets;

namespace Jn698._45GWTool.ViewModels
{
    public class CommViewModel:ViewModelBase
    {
        #region 属性
        private SerialPortHelper serialPortHelper = SerialPortHelper.Instance;
        private TcpHelper tcpHelper = TcpHelper.Instance;
        public static string selectedCommType;
        public static string selectedCom;
        private string tbComm;
        
        public string TbComm
        {
            get { return tbComm; }
            set { tbComm = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> comms;

        public ObservableCollection<string> Comms
        {
            get
            {
                if (comms == null)
                {
                    comms = new ObservableCollection<string>(SerialPortHelper.GetPortArray());
                }
                return comms;
            }
            set { comms = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<int> baudrates;

        public ObservableCollection<int> Baudrates
        {
            get
            {
                if (baudrates == null)
                {
                    baudrates = new ObservableCollection<int>() { 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 115200 };
                }
                return baudrates;
            }
            set { baudrates = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> combList;

        public ObservableCollection<string> CombList
        {
            get { return combList; }
            set { combList = value; RaisePropertyChanged(); }
        }

        private string selectedCombType;

        public string SelectedCombType
        {
            get { return selectedCombType; }
            set 
            {
                selectedCommType= value;
                selectedCombType = value;
                if (value == "串口")
                {
                    TbComm = "串口号：";
                    TbPortIndex = "0";
                }
                else
                {
                    TbComm = "端口号：";
                    TbPortIndex = "2";
                }
                RaisePropertyChanged();
            }
        }

        private int selectedbaud;

        public int SelectedBaud
        {
            get 
            {
                
                return selectedbaud; 
            }
            set { selectedbaud = value; RaisePropertyChanged(); }
        }

        private Parity selectedParity;

        public Parity SelectedParity
        {
            get 
            {
                if(selectedParity == 0)
                {
                    selectedParity=Parity.Even;
                }
                return selectedParity; 
            }
            set { selectedParity = value; RaisePropertyChanged(); }
        }

        private StopBits selectedStopBit;

        public StopBits SelectedStopBit
        {
            get 
            {
                if (selectedStopBit == 0)
                {
                    selectedStopBit = StopBits.One;
                }
                return selectedStopBit; 
            }
            set { selectedStopBit = value; RaisePropertyChanged(); }
        }

        private int selectedDataBit;

        public int SelectedDataBit
        {
            get 
            {
                if (selectedDataBit == 0)
                {
                    selectedDataBit = 8;
                }
                return selectedDataBit; 
            }
            set { selectedDataBit = value; RaisePropertyChanged(); }
        }

        
        public string SelectedCom
        {
            get 
            { 
                if (selectedCom == null&& Comms.Count>0)
                {
                    selectedCom = Comms[0];
                }
                return selectedCom; 
            }
            set { selectedCom = value; RaisePropertyChanged(); }
        }

        private int selectedPort;

        public int SelectedPort
        {
            get { return selectedPort; }
            set { selectedPort = value; }
        }


        private string tbPortIndex;

        public string TbPortIndex
        {
            get { return tbPortIndex; }
            set { tbPortIndex = value; RaisePropertyChanged(); }
        }


        public CommViewModel() 
        {
            //SelectedCombType = "串口";
            CombList = new ObservableCollection<string>() { "串口","GPRS"};
            if (Settings.CommSettings.Instanse.SelectedCombType.Equals("GPRS"))
            {
                //TbPortIndex = 3;
                TbPortIndex = "2";
                TbComm = "端口号：";
            }
            else
            {
                TbPortIndex = "0";
                TbComm = "串口号：";
            }
            //SelectedBaud = 8;
            //SelectedPort = 22070;
            /*BtnOKCommand = new RelayCommand(() =>
            {
                if (serialPortHelper.Status)
                {
                    serialPortHelper.Close();
                }
                
                if (SelectedCombType == "串口")
                {
                    try
                    {
                        //设定串口参数
                        serialPortHelper.SetSerialPort(SelectedCom, SelectedBaud, SelectedParity, SelectedDataBit, SelectedStopBit);
                    }
                    catch (Exception e1)
                    {
                        MessageBox.Show(e1.Message);
                    }
                }
                
            });*/
        }


        #endregion

        #region command
        public RelayCommand<CommWindow> BtnOKCommand 
        {
            get {
                var command = new RelayCommand<CommWindow>((window) =>
                {
                    if (serialPortHelper.Status)
                    {
                        serialPortHelper.Close();
                    }
                    if (tcpHelper.Status==ServerState.Running)
                    {
                        tcpHelper.CloseTCP();
                    }
                    SelectedCombType = Settings.CommSettings.Instanse.SelectedCombType;
                    if (SelectedCombType == "串口")
                    {
                        try
                        {

                            //设定串口参数
                            //serialPortHelper.SetSerialPort(SelectedCom, Baudrates[SelectedBaud], SelectedParity, SelectedDataBit, SelectedStopBit);
                            switch (Settings.CommSettings.Instanse.SelectedParity)
                            {
                                case "奇校验":
                                    SelectedParity = Parity.Odd;
                                    break;
                                case "偶校验":
                                    SelectedParity = Parity.Even;
                                    break;

                                case "无校验":
                                default:
                                    SelectedParity = Parity.None;
                                    break;
                            }
                            switch (Settings.CommSettings.Instanse.SelectedStopBit)
                            {
                                case "1":
                                    SelectedStopBit = StopBits.One;
                                    break;
                                case "1.5":
                                    SelectedStopBit = StopBits.OnePointFive;
                                    break;
                                case "2":
                                    SelectedStopBit = StopBits.Two;
                                    break;
                                default:
                                    SelectedStopBit = StopBits.None;
                                    break;
                            }
                            serialPortHelper.SetSerialPort(
                                Comms[Settings.CommSettings.Instanse.SelectedCom], 
                                Baudrates[Settings.CommSettings.Instanse.SelectedBaud], 
                                SelectedParity, 
                                Settings.CommSettings.Instanse.SelectedDataBit+5, 
                                SelectedStopBit);
                        }
                        catch (Exception e1)
                        {
                            MessageBox.Show(e1.Message);
                        }
                    }
                    else
                    {
                        //tcpHelper.OpenTCP(SelectedPort);
                        tcpHelper.SetTCP(Settings.CommSettings.Instanse.SelectedPort);
                    }
                    App.Current.Cfg.Save();
                    window.DialogResult = true;
                    //Messenger.Default.Send<string>("OK", "CommWindowResult");
                    
                });
                return command;
            }
            set { BtnOKCommand = value; }
        }
        public RelayCommand<CommWindow> BtnCancelCommand 
        {
            get
            {
                var command = new RelayCommand<CommWindow>((window) =>
                {

                    window.DialogResult = false;
                });
                return command;
            }
        }

        #endregion
    }
}
